﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPforRobocode
{
    [Serializable]
    class SolutionForThreeTree
    {
        const int MAXDEPTH = 10;
        const int SOL_SIZE = 6;
        List<Solution> sols;
        double _score;

       
        public double Score
        {
            get { return _score; }
            set { _score = value; }
        }
       
        public SolutionForThreeTree(int depth)
        {
            _score = -1;
            sols = new List<Solution>();
            for (int i = 0; i < SOL_SIZE; i++)
            {
                Solution temp = new Solution(depth);
                sols.Add(temp);
            }
        
        }

        public SolutionForThreeTree(Solution a, Solution b, Solution c, Solution d, Solution e, Solution f)
        {
            _score = -1;
            sols = new List<Solution>();
            
            sols.Add(a);            
            sols.Add(b);            
            sols.Add(c);
            sols.Add(d);
            sols.Add(e);
            sols.Add(f);
        }

        public void CrossOver(SolutionForThreeTree other)
        {
            for (int i = 0; i < SOL_SIZE; i++)
            {
                this.sols[i].CrossOver(other.sols[i]);
            }
        }

        public void Mutation()
        {
            for (int i = 0; i < SOL_SIZE; i++)
            {
               if(GP._rand.Next(100)<GP.MT_R) this.sols[i].Mutation();
            }
        }
        public void Eval() {
            //_score =GP._rand.Next(100);            return;
            RobotTool.GenerateCsCode("tempBot", sols[0].Show(), sols[1].Show(), sols[2].Show(), sols[3].Show2(), sols[4].Show2(), sols[5].Show2());
            RobotTool.CompileCsToDLL("tempBot");
            RobotTool.RunBattle("GpRobot.tempBot", "sample.Walls");
            
            _score = CalScore();
        }
        double CalScore()
        {
            double myScore = RobotTool.FirstTank.Score;
            double enyScore = RobotTool.SecondTank.Score;
            double esl = 0.01;
            double ret = (myScore + esl) / (myScore + enyScore + esl);
            for (int i = 0; i < SOL_SIZE; i++)
            {
                if (sols[i].Depth > MAXDEPTH) { 
                    ret -= 200;
                }
            }
            return ret;
            
        }
        
        public List<string> Show()
        {
            List<string> ret = new List<string>();
            
                 for (int i = 0; i < SOL_SIZE; i++)
            {
                ret.Add(sols[i].Show()); 
            }
            return ret;
        }
        public bool IsTooBig() {
            bool ret = false;
            for (int i = 0; i < SOL_SIZE; i++)
            {
                if (sols[i].Depth > MAXDEPTH)
                {
                    ret = true;
                }
            }
            return ret;
        }
    }
}
